package com.com.gmail.at.vssabinin.adminclient;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.InetAddress;
import java.net.UnknownHostException;


public class Main extends javax.swing.JFrame {
	private static final long serialVersionUID = 1L;

    public Main() {
        initComponents();
    }
    
    public InetAddress adress;
    
    public Client client;

    public static Main mainInstance = new Main();
    
    public boolean login = false;
    
    public void loginViewSet() {
    	mainInstance.setLocationRelativeTo(null);
        mainInstance.setVisible(true);
        mainInstance.jLabel2.setText("Hello, administrator");
    }
    
    void exit() {
    	this.client.stopClient();
    	this.dispose();
    }
   
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        logOut = new javax.swing.JButton();
        manageStation = new javax.swing.JButton();
        manageTrain = new javax.swing.JButton();
        manageSchedule = new javax.swing.JButton();
        managePassengers = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        
        this.addWindowListener( 
        	new WindowAdapter() { 
            	public void windowClosing(WindowEvent e) { 
            		exit(); 
            	} 
            } 
        );
        
        setMaximumSize(new java.awt.Dimension(290, 229));
        setMinimumSize(new java.awt.Dimension(290, 229));

        jLabel2.setText("Your name and surname:");

        logOut.setText("Log out");
        logOut.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                logOutMouseReleased(evt);
            }
        });

        manageStation.setText("Manage station database");
        manageStation.setMaximumSize(new java.awt.Dimension(155, 23));
        manageStation.setMinimumSize(new java.awt.Dimension(155, 23));
        manageStation.setPreferredSize(new java.awt.Dimension(155, 23));
        manageStation.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButton1MouseReleased(evt);
            }
        });

        manageTrain.setText("Manage train database");
        manageTrain.setMaximumSize(new java.awt.Dimension(155, 23));
        manageTrain.setMinimumSize(new java.awt.Dimension(155, 23));
        manageTrain.setPreferredSize(new java.awt.Dimension(155, 23));
        manageTrain.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButton2MouseReleased(evt);
            }
        });

        manageSchedule.setText("Manage schedule database");
        manageSchedule.setMaximumSize(new java.awt.Dimension(155, 23));
        manageSchedule.setMinimumSize(new java.awt.Dimension(155, 23));
        manageSchedule.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButton3MouseReleased(evt);
            }
        });

        managePassengers.setText("Manage passengers database");
        managePassengers.setMaximumSize(new java.awt.Dimension(155, 23));
        managePassengers.setMinimumSize(new java.awt.Dimension(155, 23));
        managePassengers.setPreferredSize(new java.awt.Dimension(155, 23));
        managePassengers.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButton4MouseReleased(evt);
            }
        });


        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(logOut)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(54, 54, 54)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)                    		 
                    	.addComponent(manageStation, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    	.addComponent(manageTrain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(manageSchedule, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(managePassengers, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE)))
                .addContainerGap(53, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(logOut))
                .addGap(18, 18, 18)
                .addComponent(manageStation, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(manageTrain, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(manageSchedule, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(managePassengers, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(31, Short.MAX_VALUE))
        );

        pack();
    }

    private void logOutMouseReleased(java.awt.event.MouseEvent evt) {
        Main.mainInstance.login = false;
        mainInstance.setVisible(false);
        Login.loginInstance.setVisible(true);
    }


    private void jButton1MouseReleased(java.awt.event.MouseEvent evt) {
        mainInstance.setVisible(false);
        StationManager.stationInstance.setLocationRelativeTo(null);
        StationManager.stationInstance.setVisible(true);
    }

    private void jButton2MouseReleased(java.awt.event.MouseEvent evt) {
    	 mainInstance.setVisible(false);
         TrainManager.trainInstance.setLocationRelativeTo(null);
         TrainManager.trainInstance.setVisible(true);
    }

    private void jButton3MouseReleased(java.awt.event.MouseEvent evt) {
    	mainInstance.setVisible(false);
        ScheduleManager.scheduleInstance.setLocationRelativeTo(null);
        ScheduleManager.scheduleInstance.setVisible(true);
    }
    
    private void jButton4MouseReleased(java.awt.event.MouseEvent evt) {
    	mainInstance.setVisible(false);
        PassengerManager.passengerInstance.setLocationRelativeTo(null);
        PassengerManager.passengerInstance.setVisible(true);
    }

    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
 
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
            	try {
                    Main.mainInstance.adress = InetAddress.getByName("localhost");
                } catch (UnknownHostException e) {
                	e.printStackTrace();
                }
            	Main.mainInstance.client = new Client();
            	if (Main.mainInstance.client.startClient(mainInstance.adress, 1015)) {
            	 	Login.loginInstance.setLocationRelativeTo(null);
                    Login.loginInstance.setVisible(true);
            	}
            }
        });
    }
    private javax.swing.JButton manageStation;
    private javax.swing.JButton manageTrain;
    private javax.swing.JButton manageSchedule;
    private javax.swing.JButton managePassengers;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JButton logOut;
}
